<?php

namespace haibao\erp\business\user;
use haibao\erp\Config;

class UserOperateHistory extends \haibao\erp\business\Base{
	
	public static function saveSearchHistory($type,$name,$page){
		$model = new \haibao\cms\model\data\SearchHistory();
		$model->Type = $type;
		$model->Keywords = $name;
		$model->PageNum = $page;
		$model->CreateTime = new \DateTime();
		$model->IpAddress = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
		$data = new \haibao\cms\data\UserOperateHistory();
		
		$data->add($model);
	}
	
	public static function getAll($filter){
		$data = new \haibao\erp\data\user\UserOperateHistory();
		return $data->getAll($filter);
	}
	
	public static function getOperateGroup(){
		return array(
			'文章操作'=>array(
				self::FUNC_CODE_ARTICLE_ADD => '添加文章',
				self::FUNC_CODE_ARTICLE_UPDATE => '编辑文章',
				self::FUNC_CODE_ARTICLE_DELETE => '删除文章'
			),
			'用户操作'=>array(
				self::FUNC_CODE_USER_ADD => '添加用户',		
				self::FUNC_CODE_USER_EDIT => '添加用户',		
				self::FUNC_CODE_USER_LOGIN => '用户登陆',		
				self::FUNC_CODE_USER_LOGOUT => '用户退出',		
				self::FUNC_CODE_USER_CHANGE_PASSWORD => '修改密码',
				self::FUNC_CODE_USERGROUP_ADD => '用户组添加',
				self::FUNC_CODE_USERGROUP_DELTE => '用户组删除',
				self::FUNC_CODE_USERGROUP_EDIT => '用户组编辑',
				self::FUNC_CODE_USERGROUP_UNLOCK => '用户组还原'
			),
			'标签操作'=>array(
				self::FUNC_CODE_TAG_ADD => '标签添加',
				self::FUNC_CODE_TAG_EDIT => '标签编辑',
				self::FUNC_CODE_TAG_DELETE => '标签删除',
			),
			'图片操作'=>array(
				self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_ADD => '图片分类添加',
				self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_EDIT => '图片分类编辑',
				self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_DELETE => '图片分类删除',
				self::FUNC_CODE_IMAGE_LIBRARY_UPLOAD => '上传图片',
				self::FUNC_CODE_IMAGE_LIBRARY_EDIT => '编辑图片',
				self::FUNC_CODE_IMAGE_LIBRARY_DELETE => '编辑图片',
				self::FUNC_CODE_IMAGE_LIBRARY_MANAGE => '图片库',
				self::FUNC_CODE_IMAGE_LIBRARY_DOWNLOAD => '下载图片'
			),
			'品牌操作'=>array(
				self::FUNC_CODE_BRAND_ADD => '添加品牌',
				self::FUNC_CODE_BRAND_SAVE => '编辑品牌',
				self::FUNC_CODE_BRAND_DELETE => '删除品牌'
			),
			'单品操作'=>array(
					self::FUNC_CODE_PRODUCT_CATEGORY_ADD => '单品类别添加',
					self::FUNC_CODE_PRODUCT_CATEGORY_SAVE => '单品类别修改',
					self::FUNC_CODE_PRODUCT_CATEGORY_DELETE => '单品类别删除',
					self::FUNC_CODE_PRODUCT_PROPERTY_ADD => '类别属性添加',
					self::FUNC_CODE_PRODUCT_PROPERTY_SAVE => '类别属性修改',
					self::FUNC_CODE_PRODUCT_PROPERTY_DELETE => '类别属性删除',
					self::FUNC_CODE_PRODUCT_ADD => '添加单品',
					self::FUNC_CODE_PRODUCT_DEL => '删除单品',
					self::FUNC_CODE_PRODUCT_UPDATE => '修改单品'
			),
				
			'明星操作' => array(
				self::FUNC_CODE_CELEBRITY_ADD => '添加明星',
				self::FUNC_CODE_CELEBRITY_EDIT => '编辑明星',
				self::FUNC_CODE_CELEBRITY_DELETE => '删除明星'
			),
			'控件操作' =>array(
				self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD => '添加控件分类',
				self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT => '编辑控件分类',
				self::FUNC_CODE_PAGE_CONTROL_CATEGORY_DELETE => '删除控件分类',
				self::FUNC_CODE_PAGE_CONTROL_ADD => '添加控件',
				self::FUNC_CODE_PAGE_CONTROL_EDIT => '编辑控件',
				self::FUNC_CODE_PAGE_CONTROL_DELETE => '删除控件'
			),
			'专题操作' => array(
				self::FUNC_CODE_FEATURE_SERIAL_ADD => '添加专题系列',
				self::FUNC_CODE_FEATURE_SERIAL_EDIT => '编辑专题系列',
				self::FUNC_CODE_FEATURE_SERIAL_DEL => '删除专题系列',
				self::FUNC_CODE_FEATURE_ADD => '添加专题',
				self::FUNC_CODE_FEATURE_EDIT => '编辑专题',
				self::FUNC_CODE_FEATURE_DEL => '删除专题'
			)
		);
	}
	
	public static function getOperateByType($operateType,$operateData){
		$info = array();
		switch($operateType){
			case self::FUNC_CODE_ARTICLE_ADD:
			case self::FUNC_CODE_ARTICLE_UPDATE:
				$jsonData = json_decode($operateData);
				if($jsonData){
					if(isset($jsonData->Title)){
						$info['data'] = $jsonData->Title;
					}
					if(isset($jsonData->PublishId)){
						$articleInfo = \haibao\cms\business\Article::getArticleIdByPublishId(array($jsonData->PublishId));
						if($articleInfo){
							$info['url'] = Config::NEW_ARTICLE_VIEW_NAME.Config::ARTICLE_VIEW_NAME.'.html?id='.$articleInfo[0];
						}
					}
				}
				$info['icon'] = 'file-alt';
				break;
			case self::FUNC_CODE_USER_LOGIN:
				$info['icon'] = 'key';
				break;
			case self::FUNC_CODE_USER_LOGOUT:
				$info['icon'] = 'off';
				break;
			
			case self::FUNC_CODE_ARTICLE_ADD:
			case self::FUNC_CODE_USER_ADD:
			case self::FUNC_CODE_USERGROUP_ADD:
			case self::FUNC_CODE_TAG_ADD:
			case self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_ADD:
			case self::FUNC_CODE_BRAND_ADD:
			case self::FUNC_CODE_PRODUCT_CATEGORY_ADD:
			case self::FUNC_CODE_PRODUCT_PROPERTY_ADD:
			case self::FUNC_CODE_PRODUCT_ADD:
			case self::FUNC_CODE_CELEBRITY_ADD:
			case self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD:
			case self::FUNC_CODE_PAGE_CONTROL_ADD:
			case self::FUNC_CODE_FEATURE_SERIAL_ADD:
			case self::FUNC_CODE_FEATURE_ADD:
			case self::FUNC_CODE_EVENT_ADD:
			case self::FUNC_CODE_ANNOUNCEMENT_ADD:
			case self::FUNC_CODE_VIDEO_ADD:
			case self::FUNC_CODE_VIDEO_CATEGORY_ADD:
			case self::FUNC_CODE_TRYOUT_ADD:
			case self::FUNC_CODE_SITE_ADD:
			case self::FUNC_CODE_SITE_CHANNEL_ADD:
			case self::FUNC_CODE_PAGE_TEMPLATE_ADD:
			case self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_ADD:
			case self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_ADD:
			case self::FUNC_CODE_INTERACTIVE_RESOURCE_ADD:
				$info['icon'] = 'save';
				break;
				
			case self::FUNC_CODE_USER_EDIT:
			case self::FUNC_CODE_USERGROUP_EDIT:
			case self::FUNC_CODE_TAG_EDIT:
			case self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_EDIT:
			case self::FUNC_CODE_IMAGE_LIBRARY_EDIT:
			case self::FUNC_CODE_BRAND_SAVE:
			case self::FUNC_CODE_PRODUCT_CATEGORY_SAVE:
			case self::FUNC_CODE_PRODUCT_PROPERTY_SAVE:
			case self::FUNC_CODE_PRODUCT_UPDATE:
			case self::FUNC_CODE_CELEBRITY_EDIT:
			case self::FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT:
			case self::FUNC_CODE_PAGE_CONTROL_EDIT:
			case self::FUNC_CODE_FEATURE_SERIAL_EDIT:
			case self::FUNC_CODE_FEATURE_EDIT:
			case self::FUNC_CODE_EVENT_SAVE:
			case self::FUNC_CODE_ANNOUNCEMENT_EDIT:
			case self::FUNC_CODE_VIDEO_SAVE:
			case self::FUNC_CODE_VIDEO_CATEGORY_EDIT:
			case self::FUNC_CODE_TRYOUT_UPDATE:
			case self::FUNC_CODE_TRYOUT_PV_NUM:
			case self::FUNC_CODE_TRYAPPLY_RECOVERY:
			case self::FUNC_CODE_TRYOUT_UPDATE_STATUS:
			case self::FUNC_CODE_SITE_EDIT:
			case self::FUNC_CODE_PAGE_TEMPLATE_EDIT:
			case self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_EDIT:
			case self::FUNC_CODE_ARTICLE_UPDATE:
			case self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_EDIT:
			case self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT:
				$info['icon'] = 'edit';
				break;
				
			case self::FUNC_CODE_USERGROUP_DELTE:
			case self::FUNC_CODE_TAG_DELETE:
			case self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_DELETE:
			case self::FUNC_CODE_IMAGE_LIBRARY_DELETE:
			case self::FUNC_CODE_BRAND_DELETE:
			case self::FUNC_CODE_PRODUCT_CATEGORY_DELETE:
			case self::FUNC_CODE_PRODUCT_PROPERTY_DELETE:
			case self::FUNC_CODE_PRODUCT_DEL:
			case self::FUNC_CODE_CELEBRITY_DELETE:
			case self::FUNC_CODE_PAGE_CONTROL_CATEGORY_DELETE:
			case self::FUNC_CODE_PAGE_CONTROL_DELETE:
			case self::FUNC_CODE_FEATURE_SERIAL_DEL:
			case self::FUNC_CODE_FEATURE_DEL:
			case self::FUNC_CODE_EVENT_DELETE:
			case self::FUNC_CODE_VIDEO_DEL:
			case self::FUNC_CODE_VIDEO_CATEGORY_DEL:
			case self::FUNC_CODE_TRYOUT_DELETE:
			case self::FUNC_CODE_TRYAPPLY_DELETE:
			case self::FUNC_CODE_SITE_DELETE:
			case self::FUNC_CODE_PAGE_TEMPLATE_DEL:
			case self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_DEL:
			case self::FUNC_CODE_ARTICLE_DELETE:
			case self::FUNC_CODE_USER_MONITOR_PAGE_DEFINE_DEL:
			case self::FUNC_CODE_INTERACTIVE_RESOURCE_DELETE:
				$info['icon'] = 'trash';
				break;
				
			default:
				$info['icon'] = 'wrench';
				break;
		}
		return $info;
	}
}
